﻿<MDataTable Headers="Headers"
            Items="_desserts"
            ItemKey="r=>r.Name"
            Class="elevation-1"
            Search="@_search"
            CustomItemFilter="FilterOnlyCapsText">
    <TopContent>
        <MTextField @bind-Value="_search"
                    Label="Search (UPPER CASE ONLY)"
                    Class="mx-4"></MTextField>
    </TopContent>
    <BodyAppendContent>
        <tr>
            <td></td>
            <td>
                <MTextField @bind-Value="_calories"
                            Type="number"
                            Label="Less than"></MTextField>
            </td>
            <td colspan="4"></td>
        </tr>
    </BodyAppendContent>
</MDataTable>


@code {

    public class Dessert
    {
        public string Name { get; set; }

        public int Calories { get; set; }

        public double Fat { get; set; }

        public int Carbs { get; set; }

        public double Protein { get; set; }

        public int Sodium { get; set; }

        public string Calcium { get; set; }

        public string Iron { get; set; }
    }

    private string _search;
    private string _calories;

    public List<DataTableHeader<Dessert>> Headers => new List<DataTableHeader<Dessert>>
    {
        new()
        {
            Text = "Dessert (100g serving)",
            Align = DataTableHeaderAlign.Start,
            Sortable = false,
            Value = nameof(Dessert.Name)
        },
        new()
        {
            Text = "Calories",
            Value = nameof(Dessert.Calories),
            Filter = (value, search, item) =>
            {
                if (string.IsNullOrEmpty(_calories))
                {
                    return true;
                }

                return item.Calories < Convert.ToInt32(_calories);
            }
        },
        new() { Text = "Fat (g)", Value = nameof(Dessert.Fat) },
        new() { Text = "Carbs (g)", Value = nameof(Dessert.Carbs) },
        new() { Text = "Protein (g)", Value = nameof(Dessert.Protein) },
        new() { Text = "Iron (%)", Value = nameof(Dessert.Iron) }
    };

    private List<Dessert> _desserts = new List<Dessert>
    {
        new Dessert
        {
            Name = "Frozen Yogurt",
            Calories = 159,
            Fat = 6.0,
            Carbs = 24,
            Protein = 4.0,
            Iron = "1%",
        },
        new Dessert
        {
            Name = "Ice cream sandwich",
            Calories = 237,
            Fat = 9.0,
            Carbs = 37,
            Protein = 4.3,
            Iron = "1%",
        },
        new Dessert
        {
            Name = "Eclair",
            Calories = 262,
            Fat = 16.0,
            Carbs = 23,
            Protein = 6.0,
            Iron = "7%",
        },
        new Dessert
        {
            Name = "Cupcake",
            Calories = 305,
            Fat = 3.7,
            Carbs = 67,
            Protein = 4.3,
            Iron = "8%",
        },
        new Dessert
        {
            Name = "Gingerbread",
            Calories = 356,
            Fat = 16.0,
            Carbs = 49,
            Protein = 3.9,
            Iron = "16%"
        },
        new Dessert
        {
            Name = "Jelly bean",
            Calories = 375,
            Fat = 0.0,
            Carbs = 94,
            Protein = 0.0,
            Iron = "0%",
        },
        new Dessert
        {
            Name = "Lollipop",
            Calories = 392,
            Fat = 0.2,
            Carbs = 98,
            Protein = 0,
            Iron = "2%",
        },
        new Dessert
        {
            Name = "Honeycomb",
            Calories = 408,
            Fat = 3.2,
            Carbs = 87,
            Protein = 6.5,
            Iron = "45%",
        },
        new Dessert
        {
            Name = "Donut",
            Calories = 452,
            Fat = 25.0,
            Carbs = 51,
            Protein = 4.9,
            Iron = "22%",
        },
        new Dessert
        {
            Name = "KitKat",
            Calories = 518,
            Fat = 26.0,
            Carbs = 65,
            Protein = 7,
            Iron = "6%",
        }
    };

    public bool FilterOnlyCapsText(object value, string search, Dessert item)
    {
        return value != null && search != null && value is string
               && value.ToString().ToUpperInvariant().IndexOf(search) != -1;
    }

}